package com.example.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.boot.bean.User;
import com.example.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author shkstart
 * @creat 2021-04-04-下午5:49
 */
@Controller
public class TableController {
    @Autowired
    UserService userService;

    @GetMapping("/basic_table")
    public String basic_table(){
        return "table/basic_table";
    }
    @GetMapping("/dynamic_table")
    public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn, Model model){
        List<User> list = userService.list();
        //第一个参数是查询第几页，第二个参数是一页查询多少个
        Page<User> userPage = new Page<>(pn, 2);
        //第二个参数是查询条件
        Page<User> page = userService.page(userPage, null);
        List<User> records = page.getRecords();
        model.addAttribute("page",page);
        return "table/dynamic_table";
    }
    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "table/responsive_table";
    }
    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }

    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable("id") Long id,
                             @RequestParam(value="pn",defaultValue = "1") Integer pn,
                             RedirectAttributes ra){
        //RedirectAttributes可以用来给重定向携带参数
        ra.addAttribute("pn",pn);
        userService.removeById(id);

        return "redirect:/dynamic_table";
    }
}
